package com.yatang.yiliao.app.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.yatang.yiliao.app.R;

import java.io.File;
import java.util.*;

public class SetInformActivity extends Activity implements AdapterView.OnItemSelectedListener {

    private int year;
    private int month;
    private int day;
    private EditText dateText;
    private List<String> list;
    private TextView sexTexts;
    public static File currentFile = null;
    private ImageView photo;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_inform);
        dateText = (EditText)findViewById(R.id.dateEdit);
        Spinner spinner=(Spinner)findViewById(R.id.spinner);
        sexTexts = (TextView)findViewById(R.id.sexText);
        photo = (ImageView)findViewById(R.id.personImage);
        Calendar mycalendar=Calendar.getInstance(Locale.CHINA);
        Date mydate=new Date(); //获取当前日期Date对象
        mycalendar.setTime(mydate);////为Calendar对象设置时间为当前日期
        year=mycalendar.get(Calendar.YEAR); //获取Calendar对象中的年
        month=mycalendar.get(Calendar.MONTH);//获取Calendar对象中的月
        day=mycalendar.get(Calendar.DAY_OF_MONTH);//获取这个月的第几天
        list=new LinkedList<String>();
        list.add("男");
        list.add("女");

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }
    public void imageOnClick(View view)
    {
        AlertDialog.Builder builder
                = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);

        View v = inflater.inflate(R.layout.activity_image_setting, null);

        builder
                .setView(v);
        dialog = builder.create();

        dialog.show();


    }

    public  void tpOnClick(View view)
    {
        dialog.dismiss();
        File dir = new File(Environment.getExternalStorageDirectory(), "bianbian");
        if(!dir.exists()) {
            dir.mkdirs();
        }
        currentFile = new File(dir, System.currentTimeMillis()+".jpg");

        Intent intent  = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentFile));
        startActivityForResult(intent, 1);
    }
    public  void pcOnClick(View view)
    {
        dialog.dismiss();
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent,
               2);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                photo.setImageURI(Uri.fromFile(currentFile));
                break;
            case 2:
                if (data != null) {
                    // 得到图片的全路径
                    Uri uri = data.getData();
                    photo.setImageURI(uri);
                }
                super.onActivityResult(requestCode, resultCode, data);
        }
    }


    public void  dateOnClick(View view)
    {
        DatePickerDialog dpd=new DatePickerDialog(SetInformActivity.this,Datelistener,year,month,day);
        dpd.show();//显示DatePickerDialog组件
    }
    private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {
            //修改year、month、day的变量值，以便以后单击按钮时，DatePickerDialog上显示上一次修改后的值
            year = myyear;
            month = monthOfYear+1;
            day = dayOfMonth;
            //更新日期
            dateText.setText(year+"/"+month+"/"+day);
        }
    };

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String sex = "";
        if (position == 0) {
            sexTexts.setText("男");
        }
        else
        {
            sexTexts.setText("女");
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public void btnFinish(View view){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }

}
